//
//  BHJsonTool.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/19.
//

import Foundation

class BHJsonTool: NSObject {
    static func decodeIntFromDic(dic: Dictionary<String,Any>?,key: String) -> Int {
        if dic != nil {
            let value = dic![key]
            if value != nil {
                if value is Int {
                    return value as! Int
                }else if value is String {
                    return Int.init(value as! String) ?? -1
                }else {
                    return -1
                }
            }else {
                return -1
            }
        }else {
            return -1
        }
    }
    
    
    static func decodeStringFromDic(dic: Dictionary<String,Any>?,key: String) -> String {
        if dic != nil {
            let value = dic![key]
            if value != nil {
                if value is Int {
                    return String(value as! Int)
                }else if value is Float {
                    return String(value as! Float)
                }else if value is Double {
                    return String(value as! Double)
                }else if value is String {
                    return value as! String
                }else {
                    return ""
                }
            }else {
                return ""
            }
        }else {
            return ""
        }
    }
    
    static func decodeDicFromDic(dic: Dictionary<String,Any>?,key: String) -> [String:Any] {
        if dic == nil {
            return [:]
        }else {
            let objc = dic![key]
            if objc != nil {
                let resultDic = objc as? Dictionary<String,Any>
                if resultDic != nil {
                  return resultDic!
                }else {
                    return [:]
                }
            }else {
                return [:]
            }
        }
    }
    
    static func decodeArrayFromDic(dic: Dictionary<String,Any>?,key: String) -> [Any] {
        if dic == nil {
            return []
        }else {
            let objc = dic![key]
            if objc != nil {
                let array = objc as? Array<Any>
                if array != nil {
                    return array!
                }else {
                    return []
                }
            }else {
                return []
            }
        }
    }
    
    
    
    // 解析hash
    static func hashDecoderString(dic: [AnyHashable: Any]?, key: String) -> String {
        if dic == nil {
            return ""
        }
        for item in dic! {
            if item.key as! String == key {
                let result = item.value as? String
                if result != nil {
                    return result!
                }else {
                    return ""
                }
            }
        }
        return ""
    }
    
    static func hashDecoderDic(dic: [AnyHashable: Any]?, key: String) -> [String:Any] {
        if dic == nil {
            return [:]
        }
        for item in dic! {
            if item.key as! String == key {
                let result = item.value as? [String:Any]
                if result != nil {
                    return result!
                }else {
                    return [:]
                }
            }
        }
        return [:]
    }
    
    static func hashDecoderInt(dic: [AnyHashable: Any]?, key: String) -> Int {
        if dic == nil {
            return -1
        }
        for item in dic! {
            if item.key as! String == key {
                let result = item.value as? Int
                if result != nil {
                    return result!
                }else {
                    return -1
                }
            }
        }
        return -1
    }
}
